package com.itheima.controller.backend;

import com.itheima.pojo.Order;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderStatusDTO;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Encounter
 * @date 2024/10/10 12:48<br/>
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController
    {
        @Autowired
        private OrderService orderService;
        
        /**
         * 动态分页查询
         *
         * @param queryPageBean 查询页面 Bean
         * @return {@link Result }
         */
        @GetMapping("/findPage")
        public Result findPage(@RequestBody QueryPageBean queryPageBean)
            {
                try
                    {
                        log.info("分页查询预约信息参数：{}", queryPageBean);
                        PageResult pageResult = orderService.queryPage(queryPageBean);
                        if (pageResult.getRows() != null && !pageResult.getRows().isEmpty())
                            {
                                return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
                    }
            }
        
        /**
         * 修改预约信息
         *
         * @param orderStatusDTO 订单状态 DTO
         * @return {@link Result }
         */
        @PutMapping("/update")
        public Result update(@RequestBody OrderStatusDTO orderStatusDTO)
            {
                try
                    {
                        log.info("修改预约信息状态：{}", orderStatusDTO);
                        Order order = new Order();
                        BeanUtils.copyProperties(orderStatusDTO, order);
                        //count记录修改成功条数
                        int count = orderService.update(order);
                        //log.info("操作成功的条数：{}", count);
                        if (count > 0)
                            {
                                return new Result(true, MessageConstant.EDIT_ORDER_STATUS_SUCCESS);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.EDIT_ORDER_STATUS_FAIL);
                            }
                    }
                catch (BeansException e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.EDIT_ORDER_STATUS_FAIL);
                    }
            }
    }
